package fr.xebia.service.impl;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Expression;

import fr.xebia.data.Client;
import fr.xebia.service.ClientService;

/**
 * Service pour l'objet Client qui permet d'effectuer les actions standards
 * (save, update, get, list, search, delete)
 */
public class ClientServiceImpl extends GenericService<Client> implements ClientService {

	/**
	 * 
	 */
	public ClientServiceImpl() {
		super();
	}

	/**
	 * @see org.xebia.service.spring.GenericService#getObjectClass()
	 */
	@Override
	protected Class<Client> getObjectClass() {
		return Client.class;
	}

	@Override
	protected void initializeChildren(Client object) {
		Hibernate.initialize(object.getContracts());
	}

	@Override
	protected void addAssociationCriteria(Criteria criteria, Client object) {
		if (object.getAdresse() != null && object.getAdresse().getId() != null) {
			logger.debug("Adding filter on adresse.id ["+ object.getAdresse().getId() + ']');
			criteria.add(Expression.eq("adresse.id", object.getAdresse().getId()));
		}
	}
}
